/**
     * 获取像素点的灰度值
     */
const wximg = function () { };

function grayPixle(pix) {
  return pix[0] * 0.299 + pix[1] * 0.587 + pix[2] * 0.114;
};

//4合1
function convert4to1(res) {
  let arr = [];
  for (let i = 0; i < res.length; i++) {
    if (i % 4 == 0) {
      let rule = 0.29900 * res[i] + 0.58700 * res[i + 1] + 0.11400 * res[i + 2];
      if (rule > 200) {
        res[i] = 0;
      } else {
        res[i] = 1;
      }
      arr.push(res[i]);
    }
  }
  return arr;
};

//8合1
function convert8to1(arr) {
  let data = [];
  for (let k = 0; k < arr.length; k += 8) {
    let temp = arr[k] * 128 + arr[k + 1] * 64 + arr[k + 2] * 32 + arr[k + 3] * 16 + arr[k + 4] * 8 + arr[k + 5] * 4 +
      arr[k + 6] * 2 + arr[k + 7] * 1
    data.push(temp);
  }
  return data;
}

/**
 * 根据微信小程序canvas组件生成符合打印要求的二维码数据
 * 打印非二维码的图片，宽度一定要是24的倍数，不然打印也会出现乱码
 */
wximg.prototype.overwriteImageData = function (data) {
  let sendWidth = data.width;
  let sendHeight = data.height;
  let arr = convert4to1(data.data);
  let sendImageData = convert8to1(arr);
  console.log("sendImageData==" + JSON.stringify(sendImageData))
  return {
    array: sendImageData,//Array.from(sendImageData),
    width: sendWidth / 8,
    height: sendHeight,
  };
};


/**
 * 采用位图方式打印二维码或图片
 * @param {Object} res //画布参数
 */
wximg.prototype.ewmImageDataImageT = function (res) {
  let width = res.width;
  let height = res.height;
  let dots = 8;
  let nl = width % 256;
  let nh = width / 256;
  let pos = []
  let m = 0
  let ch = (height + dots - 1) / dots;
  if (width > 1023) {
    console.log("==图像宽度超打印范围==")
  } else {
    let cmdByte1 = [0x1B, 0x2A, m, nl, nh]
    let cmdByte2 = [0x0A]
    let arr = convert4to1(res.data);//4合一
    let sendImageData = convert8to1(arr);//8合1

    pos.push(cmdByte1)
    pos.push(sendImageData)
    pos.push(cmdByte2)
    console.log()
    // for (let h = 0; h < ch; h++) {
    //   //------- 输出POS命令 ESC * m nl nh --------
    //   pos.push(cmdByte1)
    //   // if (sb != null) {
    //   //   posCmd(sb, cmdStr1);
    //   // } else if (baos != null) {
    //   //   posCmd(baos, cmdByte1);
    //   // }
    //   //===================END======================
    //   for (let w = 0; w < width; w++) {
    //     //值填充，白点
    //     Arrays.fill(p3, 0xFF);
    //     raster.getPixels(w, h * dots, 1,
    //       (h + 1 == ch ? height - dots * h : dots),
    //       p3);

    //     //把每8个点输出成一个字节
    //     let bytes = getBytes(p3);
    //     //把每个字节以POS命令形式输出
    //     if (sb != null) {
    //       resultStratergy(sb, bytes);
    //     } else if (baos != null) {
    //       resultStratergy(baos, bytes);
    //     }
    //   }
    //   //------- 输出POS命令 0x0A --------
    //   if (sb != null) {
    //     posCmd(sb, cmdStr2);
    //   } else if (baos != null) {
    //     posCmd(baos, cmdByte2);
    //   }
    //   //===============END=================
    // }
  }
  return pos;
};

module.exports = wximg